use lazy_static::lazy_static;

pub mod lencrypted;
pub mod ltools;
pub mod lerror;

pub mod lproperties;

/* 状态码 */
pub mod lstatuscode;

pub mod lqkey;


pub use crate::ltools::ltime;

pub fn add(left: usize, right: usize) -> usize {
    left + right
}

use std::sync::{Arc, Mutex, mpsc::{Sender}, mpsc};
use std::thread;

#[macro_export]
macro_rules! log_info {
    ($x:expr) => (
        crate::print_log(4, $x);
    );
}

#[macro_export]
macro_rules! log_error {
    ($x:expr) => (
        crate::print_log(6, $x);
    );
}

#[macro_export]
macro_rules! log_warn {
    ($x:expr) => (
        crate::print_log(5, $x);
    );
}

pub fn print_log(log_level: u8, msg: String) {
    println!("{}  {}: {}",ltime::get_now_format_time(), get_thread_info(), msg)
}
fn get_thread_info() -> String {
    let cuth = thread::current();
    let mut cname = "";
    if let Some(t) = cuth.name() { cname = t };
    format!("{:?}({:?})", cname, cuth.id())
}

lazy_static! {
    static ref sender_static:Arc<Mutex<Option<Sender<u8>>>> = Arc::new(Mutex::new(None));
}

pub fn test_init() {
    let (tx, rx) = mpsc::channel();
    *sender_static.lock().unwrap() = Some(tx);

    let rt = tokio::runtime::Builder::new_multi_thread()
        .worker_threads(num_cpus::get())
        .enable_all()
        .build()
        .expect("线程创建失败");

    std::thread::spawn(move || {
        rt.block_on(async move {
            for rx_r in rx {
                println!("当前值为： {}", rx_r);
            }
        });
    });
}

pub fn send_data(data: u8) {
    // if let Some(tx) = sender_static.clone().lock().unwrap() {
    //
    // }
    match sender_static.clone().lock().unwrap().as_ref() {
        Some(tx) => {
            println!("sender: {:?}", tx);
            tx.send(data).unwrap()
        }
        None => {}
    }
}

